import { ajax, src, util } from "../../utils/util.js";
const app = getApp();

let isIphoneX = app.globalData.isIphoneX;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uqid:'',
    navH: app.globalData.navHeight,
    currentTab:1,
    myreserve:[],
   

    page:'1',
    limit:'10',
    maxpage:"",
    count:'',
    jiazaizhong:false,
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var uqid = wx.getStorageSync("uqid");
    var type=options.type
    this.setData({
      uqid: uqid,
      
    });
    if(type){
      this.setData({
        currentTab: type,
      });
    }
    
  },

  clickTab:function(e){
    var currentTab = e.currentTarget.dataset.current;
    this.setData({
      currentTab: currentTab,
      page:1,
      myreserve:[],
    })

    this.getlist()
  },
  getlist:function(){

    var that=this
    that.setData({
      jiazaizhong: true
    })

    ajax("/api/index/reserve_list", {
      data: {
        uqid: this.data.uqid,
        type:this.data.currentTab,
        page:this.data.page,
        limit:this.data.limit,
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get',
    }, res => {
      console.log(res);
      
      if(res.error==0){
        that.data.count=res.data.count
        that.data.maxpage=Math.ceil(res.data.count/that.data.limit)
        
        if(res.data.list){
          var list=that.data.myreserve
          for (const key in res.data.list) {
            list.push(res.data.list[key])
          }
          that.setData({
            myreserve: list
          })
          
        }else{
          that.setData({
            list: []
          })
        }
        wx.hideLoading();
        that.setData({
          jiazaizhong: false
        })
      }
    })
  },

  lower(){
    console.log('触底了')
    console.log('触底了')
        var page=Number(this.data.page)+1 
				this.setData({
          page:page
        })
				if(this.data.page>this.data.maxpage){
					
					wx.showToast({
						icon:"none",
						title: '没有更多了',
						duration: 2000,
					})
				}else{
					let that = this
					wx.showLoading({
						title: '加载中...'
					});
					
					that.getlist()
					
				}
  },


  gotopay:function(e){
    var index = e.currentTarget.dataset.index;
    var info = this.data.myreserve.data[index].sin_data
    
    wx.requestPayment({
      timeStamp: info.timeStamp,
      nonceStr: info.nonceStr,
      package: info.package,
      signType: info.signType,
      paySign: info.paySign,
      success(res) {
        console.log(res)

        wx.redirectTo({
          url: '/pages/paysuccess/paysuccess',
        })
       
      },
      fail(res) {
        console.log('支付失败')
      }
    })
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var uqid = wx.getStorageSync("uqid");
    this.setData({
      uqid: uqid
    });
    this.getlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  
})